using System;
using System.Linq;
using UnityEngine;
using Wxy.Res;

/// <summary>
/// AOT入口
/// 记载热更程序集并启动
/// 下载热更资源
/// </summary>
public class GameEntry : MonoBehaviour
{
    async void Start()
    {
        ResMgr resMgr = gameObject.AddComponent<ResMgr>();
        Debug.Log("开始下载资源和dll");
        await resMgr.UpdateResAndDll();
        //等待资源更新，更新完毕加载热更入口
        LoadDll();
        var loadAsset = resMgr.LoadAsset<GameObject>("Assets/ABRes/Prefab/GameLaunch.prefab");
        var hotUpdateObj = Instantiate(loadAsset);
        if (hotUpdateObj == null)
        {
            Debug.LogError("加载资源失败");
            return;
        }
        DontDestroyOnLoad(hotUpdateObj);
    }

    private void LoadDll()
    {
#if !UNITY_EDITOR
         Assembly.Load(File.ReadAllBytes(ResCfg.DLL_PATH));
#else
        AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "HotUpdate");
#endif
    }
}